import { Injectable } from '@angular/core';
import { Dish } from '../shared/dish';
// import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs';
import { Http, Response } from '@angular/http';
import { baseURL } from '../shared/baseurl';
import { ProcessHTTPMsgService } from './process-httpmsg.service';
import { map } from "rxjs/operators";
import { catchError } from "rxjs/operators";
// import 'rxjs/add/operator/map';
// import 'rxjs/add/operator/delay';
// import 'rxjs/add/operator/catch';

@Injectable()
export class DishService {

  constructor(public http: Http,
              private processHTTPMsgService: ProcessHTTPMsgService) { }

  getDishes(): Observable<Dish[]> {
    return this.http.get(baseURL + 'dishes')
                    // .map(res => { return this.processHTTPMsgService.extractData(res); })
                    // .catch(error => { return this.processHTTPMsgService.handleError(error); });
                    .pipe(map(res => { return this.processHTTPMsgService.extractData(res); }),
                    catchError(error => { return this.processHTTPMsgService.handleError(error); }));
  }

  getDish(id: number): Observable<Dish> {
    return  this.http.get(baseURL + 'dishes/'+ id)
                    // .map(res => { return this.processHTTPMsgService.extractData(res); })
                    // .catch(error => { return this.processHTTPMsgService.handleError(error); });
                    .pipe(map(res => { return this.processHTTPMsgService.extractData(res); }),
                    catchError(error => { return this.processHTTPMsgService.handleError(error); }));
  }

  getFeaturedDish(): Observable<Dish> {
    return this.http.get(baseURL + 'dishes?featured=true')
                    // .map(res => { return this.processHTTPMsgService.extractData(res)[0]; })
                    // .catch(error => { return this.processHTTPMsgService.handleError(error); });
                    .pipe(map(res => { return this.processHTTPMsgService.extractData(res)[0]; }),
                    catchError(error => { return this.processHTTPMsgService.handleError(error); }));
  }

}